package com.cnt.controller;

import com.cnt.entity.ActivityFund;
import com.cnt.entity.FundRaise;
import com.cnt.entity.RequestRelief;
import com.cnt.service.FundService;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.util.List;

@RestController
@RequestMapping("/api/funds")
public class FundsController {

    @Autowired
    private FundService fundService;

    @PostMapping("/send")
    public ResultVo<String> sendDonation(@RequestBody FundRaise fundRaise) {

        // 处理捐款请求
        return fundService.donation(fundRaise);
    }

    @GetMapping("/getActivityFund")
    public ResultVo<ActivityFund> getActivityFund(@RequestParam(name = "ActivityFundId") Integer activityFundId) {
        // 查询活动详情
        return fundService.getActivityFund(activityFundId);
    }

    @GetMapping("/getFundRaise")
    public ResultVo<List<FundRaise>> getFundRaise(@RequestParam(name = "userId", required = false) Integer userId) {
        // 查询捐款记录
        return fundService.getFundRaise(userId);
    }

    @GetMapping("/requestReliefList")
    public ResultVo<List<RequestRelief>> getRequestReliefList(@RequestParam(name = "userId", required = false) Integer userId) {
        // 查询拨款记录
        return fundService.getRequestReliefList(userId);
    }

    @PostMapping("/createRequest")
    public ResultVo<String> createRequest(@RequestBody RequestRelief requestRelief) {
        // 提交申请
        return fundService.createRequest(requestRelief);
    }

    @PostMapping("/uploadActivityFundImage")
    public ResultVo<String> uploadActivityFundImage(@RequestParam(name = "activityFundId") Integer activityFundId,
                                               @RequestParam(name = "file") MultipartFile file) {
        // 提交活动图片
        return fundService.uploadActivityFundImage(activityFundId, file);
    }

    @PostMapping("/downloadUsedFile")
    public ResponseEntity<byte[]> downloadUsedFile(@RequestParam(name = "requestReliefId") Integer requestReliefId) {
        // 下载金钱使用资料
        try (BufferedInputStream bis = new BufferedInputStream(fundService.downloadUsedFile(requestReliefId), 4096)) {
            byte[] data = new byte[bis.available()];
            bis.read(data);
            if (data.length == 0) {
                return ResponseEntity.status(500).body(null);
            }
            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=fundUsedFile.pdf")
                    .body(data);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }

    @PostMapping("/uploadUsedFile")
    public ResultVo<String> uploadUsedFile(@RequestParam(name = "requestReliefId") Integer requestReliefId,
                                           @RequestParam(name = "usedFile") MultipartFile usedFile) {
        // 上传金钱使用资料
        return fundService.uploadUsedFile(requestReliefId, usedFile);
    }

    @PostMapping("/examine")
    public ResultVo<String> examine(@RequestBody RequestRelief requestRelief) {
        // 通过拨款
        return fundService.examineRequestFund(requestRelief);
    }

    @GetMapping("/MyRequestReliefList")
    public ResultVo<List<RequestRelief>> getMyRequestReliefList(@RequestParam(name = "userId") Integer userId) {
        // 查看我的申请
        return fundService.getMyRequestReliefList(userId);
    }

    @GetMapping("/showUser")
    public ResultVo<List<Object>> showUser() {
        // 用户查看到的四个数字
        return fundService.getUserNumbers();
    }

    @GetMapping("/showAdmin")
    public ResultVo<List<Object>> showAdmin() {
        // 管理员查看到的四个数字
        return fundService.getAdminNumbers();
    }

}
